package nc.ui.am;

import java.util.Enumeration;
import java.util.Stack;
import java.util.Vector;

import javax.swing.tree.TreeNode;

final class PreorderEnumeration implements Enumeration {
	protected Stack stack;

	public PreorderEnumeration(TreeNode rootNode) {
		super();
		Vector v = new Vector(1);
		v.addElement(rootNode); // PENDING: don't really need a vector
		stack = new Stack();
		stack.push(v.elements());
	}

	public boolean hasMoreElements() {
		return (!stack.empty() && ((Enumeration) stack.peek()).hasMoreElements());
	}

	public Object nextElement() {
		Enumeration enumer = (Enumeration) stack.peek();
		TreeNode node = (TreeNode) enumer.nextElement();
		Enumeration children = node.children();

		if (!enumer.hasMoreElements()) {
			stack.pop();
		}
		if (children.hasMoreElements()) {
			stack.push(children);
		}
		return node;
	}

}